layui.use(['element', 'table', 'form'], function () {
        var element = layui.element;
        var $ = layui.$;
        var form = layui.form;
        var table = layui.table;

















        var jsProduct = {
            'result':'1',
            'courseCstatus':5
        }
        getTable(jsProduct);
        function getTable(jsProduct) {
            console.log(jsProduct.courseCstatus);
            if(jsProduct.courseCstatus==null){
                jsProduct.courseCstatus=5;
            }
            var userId=localStorage.getItem("createId");
            jsProduct.updateId=userId;
            console.log(jsProduct);
            $.ajax({
                url: '/courseTable/getTable',
                type: 'post',
                dataType: 'json',
                data: jsProduct,
                success: function (data) {
                    tablereload(data.data);
                }
            });
        }

        //获取课程类别下拉框
        getCourseClass();
        function getCourseClass() {
            $.ajax({
                url: '/courseTable/getCourseClass',
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    var opt="<select name='classId' id='classId'>";
                    opt+="<option value=''>请选择课程分类</option>";
                    for(var da in data.data) {
                        var ba=da;
                        da++;
                        opt +="<option value='"+da+ "'>"+data.data[ba].title+"</option>"
                    }
                    opt+="</select>"
                    $("#sot").html(opt);
                    //刷新下拉框
                    form.render('select');
                }
            });
        }
        function tablereload(date) {
            table.render({
                elem: '#table',
                data: date,
                title: '数据列表',
                limit: 10,
                id: 'table',
                toolbar: '#toptool',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                    first: "首页",
                    last: "尾页",
                },
                size: 'lg',
                cols: [[//定义列
                    {type: 'checkbox'},//复选框靠左
                    {field: 'courseId', title: '课程编号', align: 'center'},
                    {
                        field: "url", title: "课程图片", align: 'center', templet: function (data) {
                            return "<img src='" + data.url + "'/>";
                        }
                    },
                    {field: 'coursePrimary', title: "课程名称", align: 'center'},
                    {field: 'coursePrimary', title: "关键字", align: 'center'},
                    {
                        field: 'courseNprice',
                        title: "课程价格",
                        align: 'center',
                        templet: function (data) {
                            return "￥" + data.courseNprice;
                        }
                    },
                    {
                        field: 'courseStatus',
                        title: "上架/下架",
                        align: 'center',
                        templet: function (data) {
                            if(data.courseStatus==2){
                                var radio = '<a href="javascript:updateStatus('+data.courseId+","+data.courseStatus+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="上架|下架" lay-filter="switchTest" value="'+data.courseStatus+'" checked></a>'
                            }else{
                                var radio = '<a href="javascript:updateStatus('+data.courseId+','+data.courseStatus+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="上架|下架" lay-filter="switchTest" value="'+data.courseStatus+'"></a>'
                            }
                            return radio;
                        }
                    },
                    {field: 'title', title: "课程分类", align: 'center'},
                    {field: 'remarks', title: "审核状态", align: 'center',
                        templet: function (data) {
                            if(data.courseCstatus==4){
                                var radio = '<p>审核通过</p>'
                            }else if(data.courseCstatus==5){
                                var radio = '<p>待审核</p>'
                            }else if(data.courseCstatus==6){
                                var radio = '<p>审核失败</p>'
                            }else{
                                var radio = '<p>未知</p>'
                            }
                            return radio;
                        }
                    },
                    {field: 'saleNum', title: "销量", align: 'center'},
                    {
                        field: '', title: "操作", align: 'center', templet: function (data) {
                            return "<div>" +
                                "<a href='javascript:getProduct(" + data.courseId + ");' style='color: #0EAA9E;margin-right: 10px'>查看</a>" +
                                "<a href='javascript:shenhe(\""+data.courseId+"\",\""+data.courseName+"\");' style='color: #0EAA9E'>审核</a><br/>"+
                                "</div>";
                        }
                    },
                ]],
            });
        }
        //审核课程
        shenhe = function shenhe(productId,productName) {
            layer.open({
                type:2,
                title:'课程审核',
                maxmin:true,
                shade:0,
                offset:'auto',
                content:"/course/course-commodity-audit.html",
                area:["450px","400px"],
                success:function (layero,index) {
                    var obj = layero.find('iframe').contents();
                    obj.find("#courseName").text(productName);
                    obj.find("#id").val(productId);
                    obj.find("#courseName").val(productName);
                }
            });
        }

        gCha = function gCha(){
            var ac={
                courseCstatus:null,
            }
            var userId=localStorage.getItem("createId");
            jsProduct.updateId=userId;
            jsProduct.courseCstatus=null;
            console.log(jsProduct);
            $.ajax({
                url: '/courseTable/getTable',
                type: 'post',
                dataType: 'json',
                data: jsProduct,
                success: function (data) {
                    tablereload(data.data);
                }
            });
        }

        shangJia = function shangJia(){
            var courseCstatus=4;
            var param={
                "courseCstatus":courseCstatus
            }
            getTable(param);
        }

        dShen = function dShen() {
            var param={
                "courseCstatus":5
            }
            getTable(param);
        }

        Bhui = function Bhui(){
            var param={
                "courseCstatus":6
            }
            getTable(param);
        }







        //搜索
        search = function search() {
            var productName = $('input[name=productName]').val();
            var courseNprice = $('input[name=courseNprice]').val();

            var classification = document.getElementById("classId");
            var indexClass = classification.selectedIndex;
            var classId = classification.options[indexClass].value;

            // var state = document.getElementById("productSate");
            // var indexState = state.selectedIndex;
            // var productSate = state.options[indexState].value;
            var productLabel = $('input[name=productLabel]').val();
            var param = {
                'coursePrimary':productName,
                'classId':classId,
                "courseNprice":courseNprice
                //'productSate':productSate
            }
            getTable(param);
        }
        //删除商品
        delCourse = function delCourse(id) {
            $.ajax({
                url: '/courseTable/delCourse',
                type: 'post',
                dataType: 'json',
                data: {'id': id},
                success: function (data) {
                    if (data) {
                        layer.msg("删除成功");
                        getTable();
                    } else {
                        layer.msg("删除成功");
                    }
                }
            });
        }
        //查看当前商品的日志
        getCoursejournal = function getCoursejournal(courseId){
            $.ajax({
                url: "/courseTable/getCoursejournal",
                type: "post",
                dataType: "json",
                data: {"courseId": courseId},
                success: function (data) {
                    localStorage.setItem("journalId",courseId);
                    layer.open({
                        type: 2,
                        title: "详情",
                        area: ['1200px', '700px'],
                        shade: 0,
                        offset: 't',
                        maxmin:true,
                        content: "/course/course-journal.html",
                        success: function (layero, index) {

                        }
                    });
                },
            })
        }

        //查看课程详情
        getProduct = function getProduct(courseId) {
            var userId=localStorage.getItem("createId");
            $.ajax({
                url: "/courseTable/getCourse",
                type: "post",
                dataType: "json",
                data: {
                    "courseId": courseId,
                    "updateId":userId
                },
                success: function (data) {
                    devicesMsg(data);
                },
            })
        }

        function devicesMsg(data) {
            var userName="";
            var userAcc="";
            layer.open({
                type: 2,
                title: "课程详情",
                area: ['990px', '650px'],
                shade: 0,
                offset: 'auto',
                maxmin: true,
                btn:['关闭'],
                content: "/course/course-see.html",
                success: function (layero, index) {
                    $.ajax({
                        url: "/login/getUser",
                        type: "post",
                        dataType: "json",
                        success: function (user) {
                            userName=user.data.trueName;
                            userAcc=user.data.account;
                            console.log(userName,userAcc);
                            var obj = layero.find('iframe').contents();
                            obj.find("#productCode").val(data.data.courseName);
                            obj.find("#productName").val(data.data.createDate);
                            obj.find("#currentpriceProduct").val(data.data.userName);
                            obj.find("#shoucang").val(data.data.collectNum);
                            obj.find("#xiaoliang").val(data.data.title);
                            obj.find("#title").val(data.data.courseNprice);
                            obj.find("#productSate").val(data.data.coursePrimary);
                            obj.find("#productLabel").val(data.data.likeNum);
                            obj.find("#hits").val(data.data.collectNum);
                            obj.find("#productDescription").val(data.data.courseIntro);
                            obj.find("#beizhu").val(data.data.courseInfo);
                            obj.find("#mainImage").attr('src',data.data.url);
                            obj.find("#Image").attr('src',data.data.courseUrl);
                            obj.find("#name").html('用户名：<input type="text"  value="'+userName+'">');
                            obj.find("#acc").html('账号：<input type="text"  value="'+userAcc+'">');
                        },
                    })

                }
            });
        }

        /*表格详情结束*/
        //开关事件
        updateStatus = function updateStatus( id,courseStatus) {
            if(courseStatus==2){
                courseStatus=3;
            }else{
                courseStatus=2;
            }
            $.ajax({
                url: '/courseTable/updateCourseStatus',
                type: 'post',
                dataType: 'json',
                data: {
                    'id': id,
                    'courseStatus': courseStatus
                },
                success:function (data) {
                    layer.msg("修改成功");
                }
            })
        }

        //查看当前商品的审核
        courseCstatus = function courseCstatus(courseId,courseCstatus){
            $.ajax({
                url: '/courseTable/getRecord',
                type: 'post',
                dataType: 'json',
                data: {
                    'courseId': courseId
                },
                success: function (data) {
                    localStorage.setItem("recId",courseId);
                    layer.open({
                        type: 2,
                        title: "详情",
                        area: ['1200px', '700px'],
                        shade: 0,
                        offset: 't',
                        maxmin:true,
                        content: "/course/course-record.html",
                        success: function (layero, index) {

                        }
                    });
                },
            })
        }
    }
);

//刷新
function Refresh() {
    location.reload();
}

//跳转
function index() {
    window.location.href = '/course/course-list.html';
}